package com.verification.com.verification;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import com.library.verification.manager.VerificationManager;
import com.library.verification.view.VerificationListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 验证码demo
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener,
        AdapterView.OnItemSelectedListener, VerificationListener {
    private Spinner spinnerType;
    private Spinner spinnerStyle;
    private Button showContent;
    private LinearLayout contentLayout;
    private VerificationManager verificationManager;

    private List<String> urls = new ArrayList<>();
    private int oldType;
    private int oldStyle;

    private final static String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&" +
            "sec=1493652430&di=d3ce6fd788df46f9a59c7bd11d0367c0&imgtype=jpg&er=1&src=http%3A%2F%2Fresource.3uol.com%2Fimages%2Fmmbb%2F2015%2F02%2FW11152410B3D53B55.jpg";

    private String[] mItemsStyle;
    private ArrayAdapter<String> adapterStyle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initPage();
    }

    private void initPage() {
        spinnerType = (Spinner) this.findViewById(R.id.spinner_type);
        spinnerStyle = (Spinner) this.findViewById(R.id.spinner_style);
        showContent = (Button) this.findViewById(R.id.show_content);
        contentLayout = (LinearLayout) this.findViewById(R.id.content_layout);

        spinnerType.setOnItemSelectedListener(this);
        spinnerStyle.setOnItemSelectedListener(this);
        showContent.setOnClickListener(this);

        String[] mItems = getResources().getStringArray(R.array.verification_type);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mItems);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerType.setAdapter(adapter);

        mItemsStyle = getResources().getStringArray(R.array.verification_img_style);

        urls.add(url);

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.show_content) {
            verificationAction();
        }
    }

    private void makeStyleContent() {
        adapterStyle = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mItemsStyle);
        adapterStyle.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerStyle.setAdapter(adapterStyle);
    }

    private void verificationAction() {
        contentLayout.removeAllViews();
        int type = spinnerType.getSelectedItemPosition();
        int style = spinnerStyle.getSelectedItemPosition();
        if (type == oldType && style == oldStyle && verificationManager != null) {
            verificationManager.show(contentLayout, urls, this);
        } else {
            oldType = type;
            oldStyle = style;
            verificationManager = VerificationManagerFactory.makeManager(this, type, style);
            verificationManager.show(contentLayout, urls, this);
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if (parent == spinnerType) {
            if (position == 0) {//图片验证
                mItemsStyle = getResources().getStringArray(R.array.verification_img_style);
            } else {//拖拽
                mItemsStyle = getResources().getStringArray(R.array.verification_drag_style);
            }
            makeStyleContent();
        }
        verificationAction();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void notifyStatusResult(int status) {
        if (status == VerificationListener.START_CHECK_STATUS) {

        } else if (status == VerificationListener.SUCCESS_STATUS) {
            Toast.makeText(this, "验证成功", Toast.LENGTH_SHORT).show();
        } else if (status == VerificationListener.FAIL_STATUS) {
            Toast.makeText(this, "验证失败", Toast.LENGTH_SHORT).show();

        }


    }
}
